/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calcmvc;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
 *
 * @author Administrator
 */
public class CalcJPanelWithNumberButtons extends JPanel implements ActionListener{
    
    private CalcController controller;
    private CalcViewer calcViewer;
    private Dimension buttonSize = new Dimension(55,55);
    
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JButton jButton7;
    private JButton jButton8;
    private JButton jButton9;
    private JButton jButton0;
    private JButton jButtonComma;
    private JButton jButtonPlusMinus;
    private JButton jButtonDivide;
    private JButton jButtonMultiply;
    private JButton jButtonMinus;
    private JButton jButtonPlus;
    private JButton jButtonEquals;
    private JButton jButtonSaveCalculation;
    private JButton jButtonLoadCalculation;
    private JButton jButtonClearSavedCalculation;
    private JButton jButtonClearSavedCalculationAndSaveCalculation;
    private JButton jButtonClear;
    private JLabel placeholder1;
    private JLabel placeholder2;
    private JLabel placeholder3;
    private JLabel placeholder4;
    private JLabel placeholder5;
    
    private GridLayout gridLayout;
    
    
    public CalcJPanelWithNumberButtons(CalcController con, CalcViewer cv)
    {
        //object creation
        controller = con;
        calcViewer = cv;
        gridLayout = new GridLayout(5,0, 10, 10);
        
       
        //variable change
        setPreferredSize(new Dimension((int)((buttonSize.getWidth() * 4) + (gridLayout.getHgap() * 3)), (int)((buttonSize.getHeight() * 5) + (gridLayout.getVgap() * 4))));
        setMaximumSize(getPreferredSize());
        setVisible(true);
       
        
        createButtons();
        setButtonSizes();
        addActionListenerToButtons();
        addContent();

       
       
    }
    
    private void addActionListenerToButtons()
    {
        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        jButton4.addActionListener(this);
        jButton5.addActionListener(this);
        jButton6.addActionListener(this);
        jButton7.addActionListener(this);
        jButton8.addActionListener(this);
        jButton9.addActionListener(this);
        jButton0.addActionListener(this);
        jButtonComma.addActionListener(this);
        jButtonPlus.addActionListener(this);
        jButtonDivide.addActionListener(this);
        jButtonMultiply.addActionListener(this);
        jButtonMinus.addActionListener(this);
        jButtonEquals.addActionListener(this);
        jButtonClear.addActionListener(this);
        jButtonSaveCalculation.addActionListener(this);
        jButtonLoadCalculation.addActionListener(this);
        jButtonClearSavedCalculation.addActionListener(this);
        jButtonClearSavedCalculationAndSaveCalculation.addActionListener(this);
        
    }
    
    
    private void setButtonSizes()
    {
    jButton1.setPreferredSize(buttonSize);
    jButtonClear.setPreferredSize(buttonSize);
    jButton2.setPreferredSize(buttonSize);
    jButton3.setPreferredSize(buttonSize);
    jButton4.setPreferredSize(buttonSize);
    jButton5.setPreferredSize(buttonSize);
    jButton6.setPreferredSize(buttonSize);
    jButton7.setPreferredSize(buttonSize);
    jButton8.setPreferredSize(buttonSize);
    jButton9.setPreferredSize(buttonSize);
    jButton0.setPreferredSize(buttonSize);
    jButtonComma.setPreferredSize(buttonSize);
    jButtonPlus.setPreferredSize(buttonSize);
    jButtonDivide.setPreferredSize(buttonSize);
    jButtonMultiply.setPreferredSize(buttonSize);
    jButtonMinus.setPreferredSize(buttonSize);
    jButtonEquals.setPreferredSize(buttonSize);
  
    placeholder1.setPreferredSize(buttonSize);
    placeholder2.setPreferredSize(buttonSize);
    placeholder5.setPreferredSize(buttonSize);
    
    jButtonSaveCalculation.setPreferredSize(buttonSize);
    jButtonLoadCalculation.setPreferredSize(buttonSize);
    jButtonClearSavedCalculation.setPreferredSize(buttonSize);
    jButtonClearSavedCalculationAndSaveCalculation.setPreferredSize(buttonSize);
    }
    
    
    private void createButtons()
    {
        jButton1 = new JButton("1");
        jButton2 = new JButton("2");
        jButton3 = new JButton("3");
        jButton4 = new JButton("4");
        jButton5 = new JButton("5");
        jButton6 = new JButton("6");
        jButton7 = new JButton("7");
        jButton8 = new JButton("8");
        jButton9 = new JButton("9");
        jButton0 = new JButton("0");
        jButtonComma = new JButton(",");
        jButtonDivide = new JButton("/");
        jButtonMultiply = new JButton("*");
        jButtonMinus = new JButton("-");
        jButtonPlus = new JButton("+");
        jButtonEquals = new JButton("=");
        jButtonClear = new JButton("C");
        placeholder1 = new JLabel();
        placeholder2 = new JLabel();
        placeholder3 = new JLabel();
        placeholder4 = new JLabel();
        placeholder5 = new JLabel();
        jButtonSaveCalculation = new JButton("M+");
        jButtonLoadCalculation = new JButton("MR");
        jButtonClearSavedCalculation = new JButton("MC");
        jButtonClearSavedCalculationAndSaveCalculation = new JButton("MS");
    }
    
    private void addContent()
    {
        add(jButtonClear);
        add(jButtonDivide);
        add(jButtonMultiply);
        add(jButtonMinus);
        add(jButton7);
        add(jButton8);
        add(jButton9);
        add(jButtonPlus);
        add(jButton4);
        add(jButton5);
        add(jButton6);
        add(jButtonClearSavedCalculationAndSaveCalculation);
        add(jButton1);
        add(jButton2);
        add(jButton3);
        add(jButtonEquals);
        add(jButton0);
        
        add(jButtonSaveCalculation);
        add(jButtonLoadCalculation);
        add(jButtonClearSavedCalculation);
        
        
            
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {
        
        JButton buttonPressed = (JButton)e.getSource();
        
        if(buttonPressed.getText().equals("/"))
        {
            controller.divideButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("C"))
        {
            controller.clearButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("*"))
        {
            controller.multiplyButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("-"))
        {
            controller.minusButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("7"))
        {
            controller.sevenButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("8"))
        {
            controller.eightButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("9"))
        {
            controller.nineButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("+"))
        {
            controller.plusButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("4"))
        {
            controller.fourButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("5"))
        {
            controller.fiveButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("6"))
        {
            controller.sixButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("1"))
        {
            controller.oneButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("2"))
        {
            controller.twoButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("3"))
        {
            controller.threeButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("="))
        {
            controller.equalsButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("0"))
        {
            controller.zeroButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("M+"))
        {
            controller.saveButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("MR"))
        {
            controller.loadButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("MC"))
        {
            controller.clearSavedCalculationButtonPressed();
        }
        
        else if(buttonPressed.getText().equals("MS"))
        {
            controller.clearSavedCalculationAndSaveCalculationButtonPressed();
        }
        
        
        
        
        else if(buttonPressed.getText().equals("^"))
        {
            controller.powerButtonPressed();
        }
    }
    

}
